//
//  OADemoTempViewController.m
//  LongOA
//
//  Created by XuJiuming on 15/10/31.
//  Copyright (c) 2015年 梁文朗. All rights reserved.
//

#import "OADemoTempViewController.h"
#import "GetOADemoTypeRequest.h"
#import "GetOADemoTypeResponse.h"
#import "GetOADemoRequest.h"
#import "GetOADemoResponse.h"
#import "OADeomTempDetailViewController.h"

@interface OADemoTempViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray* arrList;
    UITableView* _tableView;
    UITableView* _tableViewContent;
}
@property(nonatomic,strong) NSString* strAll;
@end

@implementation OADemoTempViewController
- (void)leftBarClicked:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)rightBarClicked1:(id)sender {
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavWithTitle:@"选择公文模板" createMenuItem:^UIView *(int nIndex)
     {
         float btnwidth=40.0f;
         float btnheight=40.0f;
         if (nIndex ==0)
         {
             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
             [btn setFrame:CGRectMake(10.0f, 20.0f+(self.navView.height-20 - 40)/2, btnwidth, btnheight)];
             [btn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
             [btn addTarget:self action:@selector(leftBarClicked:) forControlEvents:UIControlEventTouchUpInside];
             btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
             return btn;
         }
         if (nIndex ==2)
         {
             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
             [btn setFrame:CGRectMake(self.navView.width - btnwidth -5.0, 20.0f+(self.navView.height-20 - 40)/2, btnwidth, btnheight)];
             [btn setImage:[UIImage imageNamed:@"ic_refresh"] forState:UIControlStateNormal];
             [btn addTarget:self action:@selector(rightBarClicked1:) forControlEvents:UIControlEventTouchUpInside];
             
             return btn;
         }
         
         return nil;
     }];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 64.0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor=[UIColor clearColor];
    _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tableView.delaysContentTouches=NO;
    [self.view addSubview:_tableView];
    [self setExtraCellLineHidden:_tableView];
    
    
    _tableViewContent=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 69.0, SCREEN_WIDTH, SCREEN_HEIGHT-69) style:UITableViewStylePlain];
    _tableViewContent.delegate=self;
    _tableViewContent.dataSource=self;
    _tableViewContent.backgroundColor=[UIColor clearColor];
    _tableViewContent.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tableViewContent.delaysContentTouches=NO;
    [self.view addSubview:_tableViewContent];
    [self setExtraCellLineHidden:_tableViewContent];
    _tableViewContent.hidden=YES;
    
    [self getList:0];
}
-(void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

- (void)getList:(int)iid {
    NSString* token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    int setid=[[[NSUserDefaults standardUserDefaults] objectForKey:@"setid"] intValue];
    
    GetOADemoTypeRequest* request = [[GetOADemoTypeRequest alloc] init];
    request.token = token;
    request.parentid=iid;
    id<ILJClient> clinet = [[OAIpdataBase shareInstance] getPool:setid];
    [clinet Doexecute:request
             RspClass:[GetOADemoTypeResponse class]
               andMsg:@"获取信息..."
              andView:self.view
globalTimelinePostsWithBlock:^(id rsp) {
    GetOADemoTypeResponse* rslt = rsp;
    
    if (rslt.ErrMsg) {
        NSLog(@"%@",rslt.ErrMsg);
    }
    else{
        arrList=rslt.types;

        //NSLog(@"%@",arrList);
        [_tableView reloadData];
    }
}];
}
- (void)getOAdemo:(NSString*)longstr {
    NSString* token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    int setid=[[[NSUserDefaults standardUserDefaults] objectForKey:@"setid"] intValue];
    
    GetOADemoRequest* request = [[GetOADemoRequest alloc] init];
    request.token = token;
    request.typelongstr=longstr;
    id<ILJClient> clinet = [[OAIpdataBase shareInstance] getPool:setid];
    [clinet Doexecute:request
             RspClass:[GetOADemoResponse class]
               andMsg:@"获取信息..."
              andView:self.view
globalTimelinePostsWithBlock:^(id rsp) {
    GetOADemoResponse* rslt = rsp;
    
    if (rslt.ErrMsg) {
        NSLog(@"%@",rslt.ErrMsg);
    }
    else{
        arrList=rslt.demos;
    
        //NSLog(@"%@",rslt.demos);
        [_tableViewContent reloadData];
        _tableView.hidden=YES;
        _tableViewContent.hidden=NO;
    }
}];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark TableView delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //8.0的分割线额外设置
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        
        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
            [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==_tableView) {
        return 44.0;
    }
    return 66.0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView==_tableView) {
        static NSString *identifier = @"newCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            //点击之后，好像2-3秒才反应过来,有延迟
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            cell.backgroundColor=[UIColor clearColor];
            cell.contentView.backgroundColor=[UIColor clearColor];
        }
        
        NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
        cell.imageView.image=[UIImage imageNamed:@"ic_flow"];
        cell.textLabel.text=[dict objectForKey:@"typename"];
        
        return cell;
    }
    
    static NSString *identifier = @"newCell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        //点击之后，好像2-3秒才反应过来,有延迟
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.backgroundColor=[UIColor clearColor];
        cell.contentView.backgroundColor=[UIColor clearColor];
        
        float mleft=5.0;
        float ffont=12.0;
        UIImageView* imgView=[[UIImageView alloc] initWithFrame:CGRectMake(mleft, (66.0-32.0)/2.0,32.0, 32.0)];
        imgView.image=[UIImage imageNamed:@"ic_demo"];
        [cell.contentView addSubview:imgView];
        
        UILabel* lab2=[[UILabel alloc] initWithFrame:CGRectMake(imgView.right+5.0, 0.0, 195.0, 22.0)];
        lab2.backgroundColor=[UIColor clearColor];
        lab2.tag=11;
        lab2.textColor=[UIColor darkGrayColor];
        lab2.font=[UIFont systemFontOfSize:ffont];
        [cell.contentView addSubview:lab2];
        
        UILabel* lab1=[[UILabel alloc] initWithFrame:CGRectMake(imgView.right+5.0, lab2.bottom, 180.0, 22.0)];
        lab1.backgroundColor=[UIColor clearColor];
        lab1.tag=12;
        lab1.textColor=[UIColor darkGrayColor];
        lab1.font=[UIFont systemFontOfSize:ffont];
        [cell.contentView addSubview:lab1];
        
        UILabel* lab3=[[UILabel alloc] initWithFrame:CGRectMake(imgView.right+5.0, lab1.bottom, 180.0, 22.0)];
        lab3.backgroundColor=[UIColor clearColor];
        lab3.tag=13;
        lab3.textColor=[UIColor darkGrayColor];
        lab3.font=[UIFont systemFontOfSize:ffont];
        [cell.contentView addSubview:lab3];
        
        UIImageView* imgView1=[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-37.0, 0.0,32.0, 32.0)];
        imgView1.image=[UIImage imageNamed:@"ic_oa_flow_regular"];
        imgView1.tag=111;
        [cell.contentView addSubview:imgView1];
    }
    
    NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
    
    UILabel* lab11=(UILabel*)[cell viewWithTag:11];
    lab11.text=[NSString stringWithFormat:@"标题:%@",[dict objectForKey:@"doctitle"]];
    
    UILabel* lab12=(UILabel*)[cell viewWithTag:12];
    lab12.text=[NSString stringWithFormat:@"审批流程:%@",[dict objectForKey:@"flowname"]];

    UILabel* lab13=(UILabel*)[cell viewWithTag:13];
    lab13.text=@"备注:"; //[NSString stringWithFormat:@"备注:%@",[dict objectForKey:@""]];
    
    UIImageView* imgV=(UIImageView*)[cell viewWithTag:111];
    int iitag=[[dict objectForKey:@"if_rivet"] intValue];
    switch (iitag) {
        case 0:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_regular"];
            break;
        case 1:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_temp"];
            break;
        case 2:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_direct"];
            break;
        case 3:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_free"];
            break;
        default:
            break;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
    //NSLog(@"==%@",dict);
    if ([_strAll isEqualToString:@"OK"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"demoSelect" object:dict];
        [self dismissViewControllerAnimated:NO completion:nil];
    } else {
        int iid=[[dict objectForKey:@"sonflag"] intValue];
        if (iid==0) {
            int iparent=[[dict objectForKey:@"typeid"] intValue];
            [self getList:iparent];
        } else {
            //        if ([_strAll isEqualToString:@"OK"]) {
            //
            //
            //        } else {
            NSString* typelongstr=[dict objectForKey:@"typelongstr"];
            _strAll=@"OK";
            [self getOAdemo:typelongstr];
            //        }
        }
    }
    /*
    OADeomTempDetailViewController* vc=[[OADeomTempDetailViewController alloc] init];
    //[self.navigationController pushViewController:vc animated:NO];
    NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
    vc.typelongstr=[dict objectForKey:@"typelongstr"];

    [self presentViewController:vc animated:NO completion:nil];
     */
}

@end
